﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;


namespace Jemgine.MapEditor
{
	public partial class EditorState
	{
		public ServiceContainer Services = new ServiceContainer();
        public Jemgine.Engine.EpisodeContentManager Content;
        public String SelectedTexture
        {
            get { return Map.Header.DefaultTexture; }
        }

        public Texture2D GetTexture(String name) { return Content.GetTexture(name); }

        public bool PrepareEpisodeContent()
        {
            if (String.IsNullOrEmpty(_config.EngineFolder) || String.IsNullOrEmpty(_config.Episode))
            {
                var ISettings = new Controls.InitialSettings(_config, "Set path to editor and episode");
                var DR = ISettings.ShowDialog();
                if (DR == System.Windows.Forms.DialogResult.Cancel) return false;
            }

            bool EpisodeIsLoaded = false;

            while (!EpisodeIsLoaded)
            {

                try
                {
                    Content = new Engine.EpisodeContentManager(Services, _config.EngineFolder, _config.Episode);
                    EpisodeIsLoaded = true;
                }
                catch (Jemgine.Engine.EpisodeException e)
                {

                        var ISettings = new Controls.InitialSettings(_config, e.Message);
                        if (ISettings.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return false;
                }
            }

            Content.FrontLoadTextures();

            Jemgine.Data.ResourceEditor.InvokeResourceBrowser += (A, B) =>
                {
                    var ResourceBrowser = new Controls.ResourceDialog(A);
                    ResourceBrowser.Initialize(this, B);
                    ResourceBrowser.ShowDialog();
                    return ResourceBrowser.SelectedResource;
                };

            return true;
        }

	}
}
